package com.newland.mesdk.demo.fragment;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.newland.mesdk.demo.main.R;
import com.newland.mtype.module.common.scanner.ScannerListener;
import com.newland.mesdk.demo.interfaceimpl.ScannerInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF 扫描模块
 */
public class Scannerfragment extends Fragment implements OnClickListener {
	private MainActivity mainActivity;
	private ScannerInterfaceImpl scannerInterfaceImpl;
	private Button btnScannerInit, btnScannerStart, btnScannerStop;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_scanner, null);
		mainActivity = (MainActivity) getActivity();
		initScannerView(view);
		scannerInterfaceImpl = new ScannerInterfaceImpl();
		return view;
	}

	public void initScannerView(View view) {
		btnScannerInit = (Button) view.findViewById(R.id.btn_scanner_init);
		btnScannerInit.setOnClickListener(this);

		btnScannerStart = (Button) view.findViewById(R.id.btn_scanner_start);
		btnScannerStart.setOnClickListener(this);

		btnScannerStop = (Button) view.findViewById(R.id.btn_scanner_stop);
		btnScannerStop.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			
				mainActivity.connectDevice();
				switch (v.getId()) {
				
				case R.id.btn_scanner_init:
					if (mainActivity.processingisLocked()) {
						mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
					} else {
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始初始化扫描头" + "\r\n", MessageTag.NORMAL);
								scannerInterfaceImpl.initScan(mainActivity);
								mainActivity.showMessage("扫描头初始化成功" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("扫描初始化操作异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();
					}
					break;
				case R.id.btn_scanner_start:
					if (mainActivity.processingisLocked()) {
						mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
					} else {
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始扫描" + "\r\n", MessageTag.NORMAL);
								scannerInterfaceImpl.startScan(30, TimeUnit.SECONDS, new ScannerListener() {
									@Override
									public void onResponse(String[] strings) {
										for (String scanResult : strings) {
											mainActivity.showMessage("扫描结果：" + scanResult + "\r\n", MessageTag.DATA);
										}
									}

									@Override
									public void onFinish() {
										mainActivity.showMessage("onFinish()回调--扫描结束!" + "\r\n", MessageTag.TIP);
										
									}
								});
							} catch (Exception e) {
								mainActivity.showMessage("扫描异常：" + e.getMessage() + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();
					}
					break;
				case R.id.btn_scanner_stop:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始停止扫描" + "\r\n", MessageTag.NORMAL);
								scannerInterfaceImpl.stopScanner();
								mainActivity.showMessage("停止扫描成功" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("停止扫描异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				default:
					break;
				}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.NORMAL);
		}
	}


}
